/**
 * related to detail.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-16
 */
var page = sm("do_Page");
var app = sm("do_App");
var do_External = sm("do_External");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var orderUtil = require("url/order");
var qqHttp = require("helper/http");
var openHelper = require("helper/open");
var helperOrder = require("helper/order");
var do_LinearLayout_item = ui("do_LinearLayout_item");
var do_LinearLayout_1 = ui("do_LinearLayout_1");

var labelOrderStatus = ui("labelOrderStatus");
var iconBg = ui("iconBg");
var do_Label_consignee = ui("do_Label_consignee");
var do_Label_phone = ui("do_Label_phone");
var do_Label_address = ui("do_Label_address");
var do_Label_buyer = ui("do_Label_buyer");
var do_Label_community = ui("do_Label_community");
var do_Label_total = ui("do_Label_total");

var do_Label_orderNo = ui("do_Label_orderNo");
var do_Label_strNewAt = ui("do_Label_strNewAt");
var do_Label_payStatusAt = ui("do_Label_payStatusAt");
var do_Label_orderStatusAt = ui("do_Label_orderStatusAt");
var do_Label_sendDeliveryAt = ui("do_Label_sendDeliveryAt");
var do_Button_delivery = ui("do_Button_delivery");
var do_Button_user = ui("do_Button_user");
var layoutBtn = ui("layoutBtn");
var button_phone = ui("button_phone");
var do_Button_service = ui("do_Button_service");
var label_remark = ui("label_remark");
var label_payTypeStr = ui("label_payTypeStr");
var do_LinearLayout_Reviews = ui("do_LinearLayout_Reviews");
var orderNo = "";
var index;
var addCheckBoxItems = [];//保存checkBox

//andriod返回键
page.on("back", function() {
	app.closePage();
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "订单详情"
})
header.refreshData();

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	orderNo = page.getData().orderNo;
	if (page.getData().index) {
		index = page.getData().index;
	}
	getData(orderNo);
});
var order;
function getData(orderNo) {
	qqHttp.post(orderUtil.url.base.deliveryDetail, {orderNo:orderNo}, function(data) {
		order = data.order;
		var delivery = data.delivery;
		var sendDeliveryOrderList = data.sendDeliveryOrderList;//配送单
		labelOrderStatus.text = order.strSendDeliveryStatus;
		label_remark.text = order.remark;
		if (order.sendDeliveryStatus == 1) { //1未配送
			do_Button_delivery.visible = true;
			do_Button_delivery.text = "取件";
			if (data.canAssigned == 1) {
				do_Button_user.visible = true;
			}
			layoutBtn.visible = true;
		} else if (order.sendDeliveryStatus == 2) {//配送中
			do_Button_delivery.visible = true;
			do_Button_delivery.text = "已配送";
			do_Button_user.visible = false;
			layoutBtn.visible = true;
		} else {
			do_Button_delivery.visible = false;
			do_Button_user.visible = false;
			layoutBtn.visible = false;
		}
		/*收货地址*/
		if(null!=delivery){
			do_Label_consignee.text = "收货人："+delivery.consignee;
			do_Label_phone.text = delivery.phone;
			do_Label_address.text = "收货地址："+delivery.address;
		}
		button_phone.tag = data.buyerPhone;
		/* 配送单 */
		for(var i=0;i<sendDeliveryOrderList.length;i++) {
			var addItem = do_LinearLayout_item.add("item"+i, "source://view/sendDelivery/cell/orderDetailCell.ui");
			var orderGrid = ui(addItem+".orderGrid");
			var labelId = ui(addItem + ".labelId");
			var nameTel = ui(addItem+".nameTel");
			var labelArr = ui(addItem+".labelArr");
			var checkBox = ui(addItem+".checkBox");
			var oLabel = ui(addItem+".oLabel");
			orderGrid.items = sendDeliveryOrderList[i].itemList;
			labelId.text = sendDeliveryOrderList[i].id;
			nameTel.text = sendDeliveryOrderList[i].supplierName + '  ' + sendDeliveryOrderList[i].supplierPhone;
			labelArr.text = sendDeliveryOrderList[i].supplierAddress;
			if (sendDeliveryOrderList[i].pickupStatus == 1) {//1未取件
				checkBox.checked = true;
				addCheckBoxItems.push({"id":sendDeliveryOrderList[i].id,addItemUi:addItem});
			} else { //已配送
				checkBox.visible = false;
				oLabel.visible = true;
				oLabel.text = sendDeliveryOrderList[i].strPickupStatus+" "+sendDeliveryOrderList[i].strDeliveryStatus;
			}
		}
		do_Label_buyer.text = order.buyerNick;
		do_Label_community.text = order.communityName;
		do_Label_total.text = "￥" + order.total;
		do_Label_orderNo.text = "订单号："+order.orderNo;
		do_Label_strNewAt.text = "下单时间："+order.strNewAt;
		if (order.payStatus == 2) {
			do_Label_payStatusAt.visible = true;
			do_Label_payStatusAt.text = "付款时间："+order.strPayStatusAt; // 付款时间
		} else if(order.payStatus == 4){
			do_Label_payStatusAt.visible = true;
			do_Label_payStatusAt.text = "退款时间："+order.strPayStatusAt;
		}
		if (order.sendDeliveryStatus != 1) {
			do_Label_sendDeliveryAt.visible = true;
			do_Label_sendDeliveryAt.text = "配送时间："+order.strSendDeliveryAt;
		}
		if (order.orderStatus == 2) {
			if (order.strOrderStatusAt) {
				do_Label_orderStatusAt.visible = true;
			}
			do_Label_orderStatusAt.text = "完成时间："+order.strOrderStatusAt; // 完成时间
		}
		if (order.payType == 1) {
			label_payTypeStr.text = "货到付款";
		} else {
			label_payTypeStr.text = "在线支付";
		}
		if (data.pubFeedback) {//有评价的
			do_LinearLayout_Reviews.visible = true;
			var starList = ui("starList");
			var listdata4Star = mm("do_ListData");
			starList.bindItems(listdata4Star);
			var starOn = data.pubFeedback.score;
			listdata4Star.removeAll();
			var starListNew = [];
			for(var m=1;m<=5;m++){
				if(m <= starOn){
					starListNew.push({"starImg":"source://image/common/x_01.png"});
				}else{
					starListNew.push({"starImg":"source://image/common/x_02.png"});
				}
			}
			listdata4Star.addData(starListNew);
			starList.refreshItems();
			ui("do_GridView_CommentImg").tag = data.pubFeedback.bigImgItems;
			ui("do_GridView_CommentImg").items = data.pubFeedback.imgItems;
			ui("do_GridView_CommentImg").visible = data.pubFeedback.hasImg;
			ui("labelComment").text = data.pubFeedback.content;
			ui("CommentTime").text = data.pubFeedback.cnNewAt;
		}
		ui("do_LinearLayout_2").redraw();
	});
}


do_Button_delivery.on("touch", function(data){
	if (order.sendDeliveryStatus == 1) { //1未配送
		var sendDeliveryOrderList = [];//勾选中的
		for (var i=0; i<addCheckBoxItems.length; i++) {
			var addCheckBoxItem = addCheckBoxItems[i];
			var checked = ui(addCheckBoxItem.addItemUi+".checkBox").checked;
			if (checked) {
				var sendDeliveryOrderVo = {"id":addCheckBoxItem.id};
				sendDeliveryOrderList.push(sendDeliveryOrderVo);
			}
		}
		qqHttp.post(orderUtil.url.handle.pickuped, {orderNo:orderNo,sendDeliveryOrderList:sendDeliveryOrderList}, function(data) {
			app.closePage({"fromPage" : "sendDeliveryOrderDetail","index" : index,"sendDeliveryStatus":data.sendDeliveryStatus});
		});
	} else if (order.sendDeliveryStatus == 2) {//配送中
		qqHttp.post(orderUtil.url.handle.deliveryed, {orderNo:orderNo}, function(data) {
			app.closePage({"fromPage" : "sendDeliveryOrderDetail","index" : index,"sendDeliveryStatus":data.sendDeliveryStatus});
		});
	}
});

do_Button_user.on("touch", function(data){
	openHelper.open("source://view/sendDelivery/userList.ui",{"orderNo":orderNo});
});

button_phone.on("touch", function(data){
	if (button_phone.tag) {
		do_External.openDial(button_phone.tag);
	}
});

do_Button_service.on("touch", function(data){
	do_External.openDial("18948328120");
});